<?php

namespace Api\Controller;

use Common\Model\User\UserViewLogModel;
use Common\Model\UserCardListModel;
use Common\Util\BaseData;
use Common\Util\CmsData;
use Services\Security\SecurityService;
use Services\Upload\OssFileService;
use Services\User\UserService;
use Services\User\UserViewLogService;

class IndexController extends AuthController
{

    public function index()
    {
        $user_info = $this->checkUserUtoken();
        $app_setting = BaseData::getAppSetting();

        $card = new UserCardListModel();
        $card_find = $card->where(['user_id' => $user_info['id']])->find();
        $todayViewNumber = 0;
        $is_have_card = false;
        $view_card_log = [];
        if ($card_find) {
            $userViewLogService = new UserViewLogService();
            $card_find['square_is_show'] = $card_find['square_is_show'] == 1 ? true : false;
            $card_find['c_weixin_img'] = OssFileService::getOssUrl($card_find['c_weixin_img']);
            //有名片
            $is_have_card = true;
            //查询今天名片浏览量
            $todayViewNumber = $userViewLogService->getTodayViewNumber(UserViewLogModel::TYPE_CARD, $card_find['id']);
            $viewTodayUser = $userViewLogService->getTodayViewUser($user_info['id']);
            $viewAllUser = $userViewLogService->setParams([
                //                'model' => $card_find['id']
                'view_user_id' => $this->getUserId(),
                //                'type' => UserViewLogModel::TYPE_ALL
            ])->getViewAllUser();
            $viewAllNumber = $userViewLogService->getViewNumber(UserViewLogModel::TYPE_CARD, $card_find['id']);
        }
        $data['view_card_log'] = $viewAllUser ? $viewAllUser : [];
        $data['view_day_num'] = $todayViewNumber ? $todayViewNumber : 0; //今日访问量
        $data['view_day_person'] = $viewTodayUser ? count($viewTodayUser) : 0; //今日访客数
        $data['view_all_num'] = $viewAllNumber ? $viewAllNumber : 0; //总访问量
        $data['view_all_person'] = $viewAllUser ? count($viewAllUser) : 0; //总访客数
        $data['card_find'] = $card_find ? $card_find : [];
        $data['is_have_card'] = $is_have_card;
        $shareQueryParams = $is_have_card ? [
            'card_id' => $card_find['id'],
            'parent_user_id' => SecurityService::encryptData($user_info['id']),
        ] : [
            'parent_user_id' => SecurityService::encryptData($user_info['id']),
        ];
        $jssdk_url = $is_have_card ? $app_setting['app_domain'] . '/h5app/pages/index/lookCard?' . http_build_query($shareQueryParams) : $app_setting['app_domain'] . '/h5app/pages/index/index?' . http_build_query($shareQueryParams);
        $title = $card_find ? $card_find['c_cn_true_name'] : $user_info['user_nickname'];
        $share_info = [
            'title' => $title . '的智能名片',
            'desc' => $card_find ? $card_find['jianjie'] : '',
            'link' => $jssdk_url,
            'imgUrl' => $card_find ? $card_find['c_touxiang'] : $user_info['user_headimg'],
        ];
        $data['jssdk_param_data'] = $this->getJssdkData($jssdk_url);
        $data['share_info'] = $share_info;
        $data['user_info'] = $user_info;

        //		获取文章分类
        $article_cate_list = M("user_cms_category")->where(['user_id' => $user_info['id']])->select();
        $data['article_cate_list'] = $article_cate_list ? $article_cate_list : [];
        //教程中心链接
        $data['guide_url'] = '/pages/index/article?article_id=72&parent_user_id=10soxRKYHERgYEzrWT1NKw%3D%3D';
        $this->return_app_data(1, $data);
    }

    //	广场展示
    public function showSquare()
    {
        $this->checkUserUtoken();
        $card_id = $_POST['card_id'] ? intval($_POST['card_id']) : 0;
        $square_is_show = $_POST['square_is_show'] ? $_POST['square_is_show'] : false;
        $card = new UserCardListModel();
        $saveData = [
            'square_is_show' => $square_is_show == 'true' ? 1 : 0,
            'update_time' => time(),
        ];
        $card_find = $card->where(['id' => $card_id])->save($saveData);
        if ($card_find) {
            $this->return_app_data(1, $square_is_show == 'true' ? '名片展示' : '名片取消广场展示');
        } else {
            $this->return_app_data(0, '名片有误');
        }
    }

    //	查看他人得名片
    public function showCard()
    {
        //		判断是否已经制作了名片
        $card = new UserCardListModel();
        $card_id = $_POST['card_id'] ? intval($_POST['card_id']) : 0;
        $card_find = $card->getInfo(['id' => $card_id]);
        if ($card_find['card_status'] == 0) {
            $this->return_app_data(520, '对方未创建主页');
        }
        if ($card_find) {
            $card_find['zhiye_label'] = $card_find['zhiye_label'] ? explode(',', $card_find['zhiye_label']) : [];
            $card_find['c_weixin_img'] = OssFileService::getOssUrl($card_find['c_weixin_img']);
        }
        $user_info = (new UserService())->getUserById($card_find['user_id']);
        //记录浏览记录
        if ($this->userIsAuth()) {
            $user_info = $this->checkUserUtoken();
            //            $card->saveViewLog($user_info['id'], $card_id);
        }
        //判断当前用户是否已经有名片了
        $self_card_info = $card->getInfo(['user_id' => $user_info['id']]);
        //		获取文章分类
        $article_cate_list = M("user_cms_category")->where(['user_id' => $card_find['user_id']])->select();
        $app_setting = BaseData::getAppSetting();
        $shareQueryParams = [
            'card_id' => $card_find['id'],
            'parent_user_id' => SecurityService::encryptData($user_info['id']),
        ];
        $jssdk_url = $app_setting['app_domain'] . '/h5app/pages/index/lookCard?' . http_build_query($shareQueryParams);
        $this->return_app_data(1, [
            'card_find' => $card_find ? $card_find : false,
            'self_card' => $self_card_info ? true : false,
            'article_cate_list' => $article_cate_list ? $article_cate_list : [],
            'user_is_auth' => $this->userIsAuth(),
            'jssdk_param_data' => $this->getJssdkData($jssdk_url),
            'share_info' => [
                'title' => $card_find['c_cn_true_name'] . '的智能名片',
                'desc' => $card_find ? $card_find['jianjie'] : '',
                'link' => $jssdk_url,
                'imgUrl' => $card_find ? $card_find['c_touxiang'] : $user_info['user_headimg'],
            ]
        ]);
    }

    public function getAdList()
    {
        $this->checkUserUtoken();
        $cate_id = $_POST['cate_id'] ? intval($_POST['cate_id']) : 0;
        $ad_list = CmsData::getAdList($cate_id);
        $this->return_app_data(1, $ad_list);
    }

    public function jssdkParamData()
    {
        $app_setting = BaseData::getAppSetting();
        $jssdk_url = $app_setting['beiyong_app_domain'] . '/h5app/pages/index/index';
        $sdk_url = $_POST['go_url'] ? $_POST['go_url'] : $jssdk_url;
        //        分享域名使用主域名，浏览页面使用备用域名
        if (strpos($jssdk_url, $app_setting['beiyong_app_domain']) !== false) {
            $sdk_url = str_replace($app_setting['beiyong_app_domain'], $app_setting['app_domain'], $sdk_url);
        }
        $share_info = [
            'title' => $app_setting['app_share_title'],
            'desc' => $app_setting['app_share_info'],
            'link' => $sdk_url,
            'imgUrl' => $app_setting['app_logo'],
        ];
        $data['jssdk_param_data'] = $this->getJssdkData($sdk_url);
        $data['share_info'] = $share_info;
        $this->return_app_data(1, $data);
    }
}
